<?php
/**
 * 个人中心-杂
 * Created by PhpStorm.
 * User: Earl <guo@seosite.cn>
 * Date: 2017/9/27
 * Time: 上午10:14
 * Updatetime:
 * updater：
 */
namespace WAdmin\Controller;


use lyf\Tree;

class CenterController extends BaseController{

    /**
     * @desc 消息类型：1 视频，2 音频，3 报告，4 图片，5 快讯，6 数据，7 会议，8 专栏，9 问答，10 问卷
     * @param $k
     */
    private function messageTypeText($k)
    {
        $list[0] = '全部';
        $list[1] = '报警信息';
        $list[2] = '系统消息';
        return $list[$k];
    }

    /**
     * @desc 消息列表
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function msgList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('type');

        unset($check);

        $uid = $this->userInfo->id;
        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('Message');
        $map['uid'] = $uid;
        if (!empty($param['type'])) $map['type'] = $param['type'];

        list($listRet, $page) = _listPage($model, $map, $p, 'is_read asc , create_time desc', 'id,title,content,is_read,type,create_time', $listRows);
        if (!empty($listRet)){
            foreach ($listRet as &$v){
                $v['typeText'] = $this->messageTypeText($v['type']);
            }
        }
        return showList($listRet, $page);
    }

    /**
     * @desc 全部设为已读
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @return array
     */
    public function setMsgAllRead()
    {
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        unset($check);

        $uid = $this->userInfo->id;
        if (empty($uid)) return showErrorMsg(L('_TOKEN_USELESS_'),100004);
        M('message')->where(array('user_id'=>$uid,'is_read'=>0))->setField('read_time',NOW_TIME);
        M('message')->where(array('user_id'=>$uid,'is_read'=>0))->setField('is_read',1);
        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,new \stdClass());
    }

    /**
     * @desc 设置消息为已读
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function setMsgRead($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $id = $param['id'];

        $operation = M('message')->where(array('id'=>array('in',$id)))->setField('read_time',NOW_TIME);
        $operation = M('message')->where(array('id'=>array('in',$id)))->setField('is_read',1);

        return showErrorMsg(L('_OPERATION_SUCCESS_'));
    }


    /**
     * @desc 消息详情
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function msgDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $id = $param['id'];
        $msgDetail = M('message')->field('id,title,content,create_time')->find($id);
        if (empty($msgDetail)) return showErrorMsg('消息不存在',100002);
        if (empty($msgDetail['is_read'])){
            M('message')->where(array('id'=>$msgDetail['id']))->setField('read_time',NOW_TIME);
            M('message')->where(array('id'=>$msgDetail['id']))->setField('is_read',1);
        }
        $msgDetail['create_time'] = date('Y-m-d',$msgDetail['create_time']);
        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,$msgDetail);
    }

    /**
     * @desc 删除消息
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function delMsg($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $id = $param['id'];
        $operation = M('message')->where(array('id'=>array('in',$id)))->delete();
        if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
        return showErrorMsg(L('_OPERATION_FAIL_'),100002);
    }

    /**
     * @desc 围栏列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('enclosure');

        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,$p,'create_time desc','id,title',$listRows);

        return showList($list,$page);
    }

    /**
     * @desc 围栏详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('enclosure');

        $info = M("enclosure")->where(['id'=>$param['id'],'uid'=>$this->userInfo->id])->find();

        if (empty($info)) return showErrorMsg(L("_OPERATION_FAIL_").':'.L('_DATA_NOT_EXIT_'),100002);

        $info['content'] = !empty($info) ? unserialize($info['content']) : [];

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$info);
    }

    /**
     * @desc 添加围栏
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('title,shape,content');

        unset($check);

        $model = M('enclosure');

        $insert_data = array(

            'uid'       =>  $this->userInfo->id,
            'title'     =>  $param['title'],
            'shape'     =>  $param['shape'],
            'content'   =>  serialize($param['content']),
            'create_time'   =>  $param['create_time'],

        );

        $operation = $model->add($insert_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 删除围栏
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureDelete($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('enclosure');

        $info = M("enclosure")->where(['id'=>$param['id'],'uid'=>$this->userInfo->id])->find();

        if (empty($info)) return showErrorMsg(L("_OPERATION_FAIL_").':'.L('_DATA_NOT_EXIT_'),100002);

        $operation = M("enclosure")->where(['id'=>$param['id'],'uid'=>$this->userInfo->id])->delete();

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 汽车列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('car');

        if (!empty($param['keyword'])){
            $condition['title'] = array('like','%'.$param['keyword'].'%');
            $condition['car_no'] = array('like','%'.$param['keyword'].'%');
            $condition['equipment_no'] = array('like','%'.$param['keyword'].'%');
            $condition['sim_no'] = array('like','%'.$param['keyword'].'%');
            $condition['_logic'] ='or';
            $map['_complex'] = $condition;
        }
        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,$p,'create_time desc','id,car_no,headsmall,equipment_no,sim_no,last_call_time',$listRows);

        return showList($list,$page);
    }

    /**
     * @desc 删除汽车
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carDelete($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('ids');

        unset($check);

        $ids = $param['ids'];

        $model = M('car');

        foreach (explode(',',$ids) as $k=>$v){

            $carInfo = $model->where(['id'=>$v,'uid'=>$this->userInfo->id])->find();
            if (empty($carInfo)) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);
        }


        $map['id'] = array('in',$param['ids']);

        $operation = $model->where($map)->delete();

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 汽车详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('car');

        $info = $model->where(['id'=>$param['id'],'uid'=>$this->userInfo->id])->find();

        if (empty($info)) return showErrorMsg(L("_OPERATION_FAIL_").':'.L('_DATA_NOT_EXIT_'),100002);


        $info['g_title'] = M('car_group')->where(['car_id'=>$info['id']])->getField('title');
        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$info);
    }

    /**
     * @desc 汽车添加
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('car');

        $insert_data['uid'] = $this->userInfo->id;

        if (!empty($param['car_no'])) $insert_data['car_no'] = $param['car_no'];
        if (!empty($param['headsmall'])) $insert_data['headsmall'] = $param['headsmall'];
        if (!empty($param['factory_no'])) $insert_data['factory_no'] = $param['factory_no'];
        if (!empty($param['factor'])) $insert_data['factor'] = $param['factor'];
        if (!empty($param['car_type'])) $insert_data['car_type'] = $param['car_type'];
        if (!empty($param['operation_license'])) $insert_data['operation_license'] = $param['operation_license'];
        if (!empty($param['color'])) $insert_data['color'] = $param['color'];
        if (!empty($param['engine_no'])) $insert_data['engine_no'] = $param['engine_no'];
        if (!empty($param['chassis_no'])) $insert_data['chassis_no'] = $param['chassis_no'];
        if (!empty($param['enterprise_name'])) $insert_data['enterprise_name'] = $param['enterprise_name'];
        if (!empty($param['length'])) $insert_data['length'] = $param['length'];
        if (!empty($param['volume'])) $insert_data['volume'] = $param['volume'];
        if (!empty($param['route_site'])) $insert_data['route_site'] = $param['route_site'];
        if (!empty($param['container_no'])) $insert_data['container_no'] = $param['container_no'];
        if (!empty($param['container_via_no'])) $insert_data['container_via_no'] = $param['container_via_no'];
        if (!empty($param['refrigeration_no'])) $insert_data['refrigeration_no'] = $param['refrigeration_no'];
        if (!empty($param['refrigeration_serial_no'])) $insert_data['refrigeration_serial_no'] = $param['refrigeration_serial_no'];
        if (!empty($param['frame_no'])) $insert_data['frame_no'] = $param['frame_no'];
        if (!empty($param['sim_no'])) $insert_data['sim_no'] = $param['sim_no'];
        if (!empty($param['equipment_no'])) $insert_data['equipment_no'] = $param['equipment_no'];
        if (!empty($param['car_model'])) $insert_data['car_model'] = $param['car_model'];

        $operation = $model->add($insert_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);

    }

    /**
     * @desc 一次性汽车添加
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carOneAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('car');

        $insert_data['uid'] = $this->userInfo->id;

        if (!empty($param['car_no'])) $insert_data['car_no'] = $param['car_no'];
        if (!empty($param['headsmall'])) $insert_data['headsmall'] = $param['headsmall'];
        if (!empty($param['factory_no'])) $insert_data['factory_no'] = $param['factory_no'];
        if (!empty($param['factor'])) $insert_data['factor'] = $param['factor'];
        if (!empty($param['car_type'])) $insert_data['car_type'] = $param['car_type'];
        if (!empty($param['operation_license'])) $insert_data['operation_license'] = $param['operation_license'];
        if (!empty($param['color'])) $insert_data['color'] = $param['color'];
        if (!empty($param['engine_no'])) $insert_data['engine_no'] = $param['engine_no'];
        if (!empty($param['chassis_no'])) $insert_data['chassis_no'] = $param['chassis_no'];
        if (!empty($param['enterprise_name'])) $insert_data['enterprise_name'] = $param['enterprise_name'];
        if (!empty($param['length'])) $insert_data['length'] = $param['length'];
        if (!empty($param['volume'])) $insert_data['volume'] = $param['volume'];
        if (!empty($param['route_site'])) $insert_data['route_site'] = $param['route_site'];
        if (!empty($param['container_no'])) $insert_data['container_no'] = $param['container_no'];
        if (!empty($param['container_via_no'])) $insert_data['container_via_no'] = $param['container_via_no'];
        if (!empty($param['refrigeration_no'])) $insert_data['refrigeration_no'] = $param['refrigeration_no'];
        if (!empty($param['refrigeration_serial_no'])) $insert_data['refrigeration_serial_no'] = $param['refrigeration_serial_no'];
        if (!empty($param['frame_no'])) $insert_data['frame_no'] = $param['frame_no'];
        if (!empty($param['sim_no'])) $insert_data['sim_no'] = $param['sim_no'];
        if (!empty($param['equipment_no'])) $insert_data['equipment_no'] = $param['equipment_no'];
        if (!empty($param['car_model'])) $insert_data['car_model'] = $param['car_model'];

        $operation = $model->add($insert_data);

        if ($operation) {
            $model_e = M('car_equipment');

            $insert_data_e = [
                'car_id'                =>  $operation,
                'uid'                   =>  $this->userInfo->id,
                'speed'                 =>  empty($param['e_speed'])?0:$param['e_speed'],
                'outside_area'          =>  empty($param['e_outside_area'])?0:$param['e_outside_area'],
                'inside_area'           =>  empty($param['e_inside_area'])?0:$param['e_inside_area'],
                'battery_dismantle'     =>  empty($param['e_battery_dismantle'])?0:$param['e_battery_dismantle'],
                'temperature'           =>  empty($param['e_temperature'])?0:$param['e_temperature'],
                'unlawful_opening'      =>  empty($param['e_unlawful_opening'])?0:$param['e_unlawful_opening'],
                'parking_overtime'      =>  empty($param['e_parking_overtime'])?0:$param['e_parking_overtime'],
                'offline'               =>  empty($param['e_offline'])?0:$param['e_offline'],
                'online'                =>  empty($param['e_online'])?0:$param['e_online'],
                'reversal'              =>  empty($param['e_reversal'])?0:$param['e_reversal'],
                'shock'                 =>  empty($param['e_shock'])?0:$param['e_shock'],
                'low_voltage'           =>  empty($param['e_low_voltage'])?0:$param['e_low_voltage'],
                'ignition'              =>  empty($param['e_ignition'])?0:$param['e_ignition'],
                'stop_ignition'         =>  empty($param['e_stop_ignition'])?0:$param['e_stop_ignition'],
                'temperature_sensor'    =>  empty($param['e_temperature_sensor'])?0:$param['e_temperature_sensor'],
                'gate_magnetic'         =>  empty($param['e_gate_magnetic'])?0:$param['e_gate_magnetic'],
                'ignition_line'         =>  empty($param['e_ignition_line'])?0:$param['e_ignition_line'],
                'oil_sensor'            =>  empty($param['e_oil_sensor'])?0:$param['e_oil_sensor'],
            ];

            $operation_e = $model_e->add($insert_data_e);

            if ($operation_e) {

                $model_s = M('car_show');

                $insert_data_s = [
                    'car_id'                =>  $operation,
                    'uid'                   =>  $this->userInfo->id,
                    'equipment_title'                 =>  empty($param['s_equipment_title'])?0:$param['s_equipment_title'],
                    'gps_time'          =>  empty($param['s_gps_time'])?0:$param['s_gps_time'],
                    'speed'           =>  empty($param['s_speed'])?0:$param['s_speed'],
                    'status'     =>  empty($param['s_status'])?0:$param['s_status'],
                    'location'           =>  empty($param['s_location'])?0:$param['s_location'],
                    'temperature'      =>  empty($param['s_temperature'])?0:$param['s_temperature'],
                    'humidity'      =>  empty($param['s_humidity'])?0:$param['s_humidity'],
                    'entrance_guard'               =>  empty($param['s_entrance_guard'])?0:$param['s_entrance_guard'],
                    'refrigerator'                =>  empty($param['s_refrigerator'])?0:$param['s_refrigerator'],
                    'oil'              =>  empty($param['s_oil'])?0:$param['s_oil'],
                    'mileage_now'                 =>  empty($param['s_mileage_now'])?0:$param['s_mileage_now'],
                    'mileage_all'           =>  empty($param['s_mileage_all'])?0:$param['s_mileage_all']
                ];

                $operation_s = $model_s->add($insert_data_s);

                if ($operation_s) {
                    if (!empty($param['gid'])){
                        $model->where(['id'=>$operation])->setField('gid',$param['gid']);
                    }
                    return showErrorMsg(L("_OPERATION_SUCCESS_"));
                }else{
                    $model->delete($operation);
                    $model_e->delete($operation_e);
                    return showErrorMsg(L("_OPERATION_FAIL_"),10002);
                }
            }else{
                $model->delete($operation);
                return showErrorMsg(L("_OPERATION_FAIL_"),10002);
            }
        }
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 汽车修改
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carUpdate($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('car');

        $save_data['id'] = $param['id'];

        if (!empty($param['car_no'])) $save_data['car_no'] = $param['car_no'];
        if (!empty($param['headsmall'])) $save_data['headsmall'] = $param['headsmall'];
        if (!empty($param['factory_no'])) $save_data['factory_no'] = $param['factory_no'];
        if (!empty($param['factor'])) $save_data['factor'] = $param['factor'];
        if (!empty($param['car_type'])) $save_data['car_type'] = $param['car_type'];
        if (!empty($param['operation_license'])) $save_data['operation_license'] = $param['operation_license'];
        if (!empty($param['color'])) $save_data['color'] = $param['color'];
        if (!empty($param['engine_no'])) $save_data['engine_no'] = $param['engine_no'];
        if (!empty($param['chassis_no'])) $save_data['chassis_no'] = $param['chassis_no'];
        if (!empty($param['enterprise_name'])) $save_data['enterprise_name'] = $param['enterprise_name'];
        if (!empty($param['length'])) $save_data['length'] = $param['length'];
        if (!empty($param['volume'])) $save_data['volume'] = $param['volume'];
        if (!empty($param['route_site'])) $save_data['route_site'] = $param['route_site'];
        if (!empty($param['container_no'])) $save_data['container_no'] = $param['container_no'];
        if (!empty($param['container_via_no'])) $save_data['container_via_no'] = $param['container_via_no'];
        if (!empty($param['refrigeration_no'])) $save_data['refrigeration_no'] = $param['refrigeration_no'];
        if (!empty($param['refrigeration_serial_no'])) $save_data['refrigeration_serial_no'] = $param['refrigeration_serial_no'];
        if (!empty($param['frame_no'])) $save_data['frame_no'] = $param['frame_no'];
        if (!empty($param['sim_no'])) $save_data['sim_no'] = $param['sim_no'];
        if (!empty($param['equipment_no'])) $save_data['equipment_no'] = $param['equipment_no'];
        if (!empty($param['car_model'])) $save_data['car_model'] = $param['car_model'];

        $operation = $model->save($save_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 汽车分组列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carGroupList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('car_group');

        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,$p,'sort asc ,update_time desc,create_time desc','id,title',$listRows);

        $model_car = M('car');
        if (!empty($list)){
            foreach ($list as $k=>$v){
                $car_list = $model_car->field('id,headsmall,car_no,equipment_no,sim_no,last_call_time')->where(['gid'=>$v['id']])->select();
                $list[$k]['_child'] = empty($car_list)?[]:$car_list;
            }
        }
        return showErrorMsg($list,$page);
    }

    /**
     * @desc 添加群
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carGroupAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('title');

        unset($check);


        $insert_data = [
            'create_time' => time(),
            'update_time' => time(),
            'uid'   =>  $this->userInfo->id,
            'title' =>  $param['title'],
            'sort'  =>  50
        ];

        $model = M('car_group');

        $operation = $model->add($insert_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 删除群
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carGroupDelete($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('car_group');

        $is_car_group_exit = $model->where(['uid'=>$this->userInfo->id,'id'=>$param['id']])->find();
        if (!$is_car_group_exit) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);

        $operation = $model->delete($param['id']);

        if($operation){
            M('car')->where(['gid'=>$param['id']])->setField('gid','0');
            return showErrorMsg(L("_OPERATION_SUCCESS_"));
        }else{
            return showErrorMsg(L("_OPERATION_FAIL_"),10002);
        }

    }

    /**
     * @desc 添加车到群里
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carGroupAddCar($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('group_id,car_id');

        unset($check);

        $model = M('car_group');

        $is_car_group_exit = $model->where(['uid'=>$this->userInfo->id,'id'=>$param['group_id']])->find();
        if (!$is_car_group_exit) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);

        $model_car = M('car');

        $is_car_exist = $model_car->where(['uid'=>$this->userInfo->id,'id'=>$param['car_id']])->find();
        if (!$is_car_exist) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);


        $operation = $model_car->where(['id'=>$param['car_id']])->setField('gid',$param['group_id']);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 汽车设备详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carEquipmentDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('car');
        $is_car_exit = $model->where(['uid'=>$this->userInfo->id,'id'=>$param['id']])->find();
        if (!$is_car_exit) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);

        $carEquipmentInfo = M('car_equipment')->where(['car_id'=>$param['id']])->find();

        if (!$carEquipmentInfo) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);


        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$carEquipmentInfo);
    }


    /**
     * @desc 汽车设备报警信息添加
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carEquipmentAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('car_id');

        unset($check);


        $model = M('car_equipment');

        $is_exist = $model->where(['car_id'=>$param['car_id']])->find();

        if ($is_exist) return showErrorMsg(L("_OPERATION_FAIL_").':'.L("_DATA_EXIST_"),100002,2,$is_exist);

        $insert_data = [
            'car_id'                =>  $param['car_id'],
            'uid'                   =>  $this->userInfo->id,
            'speed'                 =>  empty($param['speed'])?0:$param['speed'],
            'outside_area'          =>  empty($param['outside_area'])?0:$param['outside_area'],
            'inside_area'           =>  empty($param['inside_area'])?0:$param['inside_area'],
            'battery_dismantle'     =>  empty($param['battery_dismantle'])?0:$param['battery_dismantle'],
            'temperature'           =>  empty($param['temperature'])?0:$param['temperature'],
            'unlawful_opening'      =>  empty($param['unlawful_opening'])?0:$param['unlawful_opening'],
            'parking_overtime'      =>  empty($param['parking_overtime'])?0:$param['parking_overtime'],
            'offline'               =>  empty($param['offline'])?0:$param['offline'],
            'online'                =>  empty($param['online'])?0:$param['online'],
            'reversal'              =>  empty($param['reversal'])?0:$param['reversal'],
            'shock'                 =>  empty($param['shock'])?0:$param['shock'],
            'low_voltage'           =>  empty($param['low_voltage'])?0:$param['low_voltage'],
            'ignition'              =>  empty($param['ignition'])?0:$param['ignition'],
            'stop_ignition'         =>  empty($param['stop_ignition'])?0:$param['stop_ignition'],
            'temperature_sensor'    =>  empty($param['temperature_sensor'])?0:$param['temperature_sensor'],
            'gate_magnetic'         =>  empty($param['gate_magnetic'])?0:$param['gate_magnetic'],
            'ignition_line'         =>  empty($param['ignition_line'])?0:$param['ignition_line'],
            'oil_sensor'            =>  empty($param['oil_sensor'])?0:$param['oil_sensor'],
        ];

        $operation = $model->add($insert_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 汽车设备报警修改
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carEquipmentUpdate($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);


        $model = M('car_equipment');

        $is_exist = $model->where(['id'=>$param['id']])->find();

        if (!$is_exist) return showErrorMsg(L("_OPERATION_FAIL_").':'.L("_DATA_NOT_EXIT_"),100002);

        $save_data['id'] = $param['id'];
        if (!empty($param['speed'])) $save_data['speed'] = $param['speed'];
        if (!empty($param['outside_area'])) $save_data['outside_area'] = $param['outside_area'];
        if (!empty($param['inside_area'])) $save_data['inside_area'] = $param['inside_area'];
        if (!empty($param['battery_dismantle'])) $save_data['battery_dismantle'] = $param['battery_dismantle'];
        if (!empty($param['temperature'])) $save_data['temperature'] = $param['temperature'];
        if (!empty($param['unlawful_opening'])) $save_data['unlawful_opening'] = $param['unlawful_opening'];
        if (!empty($param['parking_overtime'])) $save_data['parking_overtime'] = $param['parking_overtime'];
        if (!empty($param['offline'])) $save_data['offline'] = $param['offline'];
        if (!empty($param['online'])) $save_data['online'] = $param['online'];
        if (!empty($param['reversal'])) $save_data['reversal'] = $param['reversal'];
        if (!empty($param['shock'])) $save_data['shock'] = $param['shock'];
        if (!empty($param['low_voltage'])) $save_data['low_voltage'] = $param['low_voltage'];
        if (!empty($param['ignition'])) $save_data['ignition'] = $param['ignition'];
        if (!empty($param['stop_ignition'])) $save_data['stop_ignition'] = $param['stop_ignition'];
        if (!empty($param['temperature_sensor'])) $save_data['temperature_sensor'] = $param['temperature_sensor'];
        if (!empty($param['gate_magnetic'])) $save_data['gate_magnetic'] = $param['gate_magnetic'];
        if (!empty($param['ignition_line'])) $save_data['ignition_line'] = $param['ignition_line'];
        if (!empty($param['oil_sensor'])) $save_data['oil_sensor'] = $param['oil_sensor'];


        $operation = $model->save($save_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 汽车浮动消息设置详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carFloatDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('car');
        $is_car_exit = $model->where(['uid'=>$this->userInfo->id,'id'=>$param['id']])->find();
        if (!$is_car_exit) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);

        $carEquipmentInfo = M('car_show')->where(['car_id'=>$param['id']])->find();

        if (!$carEquipmentInfo) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);


        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$carEquipmentInfo);
    }

    /**
     * @desc 汽车浮动消息添加
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carFloatAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('car_id');

        unset($check);


        $model = M('car_show');

        $is_exist = $model->where(['car_id'=>$param['car_id']])->find();

        if ($is_exist) return showErrorMsg(L("_OPERATION_FAIL_").':'.L("_DATA_EXIST_"),100002,2,$is_exist);

        $insert_data = [
            'car_id'                =>  $param['car_id'],
            'uid'                   =>  $this->userInfo->id,
            'equipment_title'                 =>  empty($param['equipment_title'])?0:$param['equipment_title'],
            'gps_time'          =>  empty($param['gps_time'])?0:$param['gps_time'],
            'speed'           =>  empty($param['speed'])?0:$param['speed'],
            'status'     =>  empty($param['status'])?0:$param['status'],
            'location'           =>  empty($param['location'])?0:$param['location'],
            'temperature'      =>  empty($param['temperature'])?0:$param['temperature'],
            'humidity'      =>  empty($param['humidity'])?0:$param['humidity'],
            'entrance_guard'               =>  empty($param['entrance_guard'])?0:$param['entrance_guard'],
            'refrigerator'                =>  empty($param['refrigerator'])?0:$param['refrigerator'],
            'oil'              =>  empty($param['oil'])?0:$param['oil'],
            'mileage_now'                 =>  empty($param['mileage_now'])?0:$param['mileage_now'],
            'mileage_all'           =>  empty($param['mileage_all'])?0:$param['mileage_all']
        ];

        $operation = $model->add($insert_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 汽车浮动消息修改
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carFloatUpdate($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);


        $model = M('car_show');

        $is_exist = $model->where(['id'=>$param['id']])->find();

        if (!$is_exist) return showErrorMsg(L("_OPERATION_FAIL_").':'.L("_DATA_NOT_EXIT_"),100002,2,$is_exist);

        $save_data['id'] = $param['id'];
        if (!empty($param['equipment_title'])) $save_data['equipment_title'] = $param['equipment_title'];
        if (!empty($param['gps_time'])) $save_data['gps_time'] = $param['gps_time'];
        if (!empty($param['speed'])) $save_data['speed'] = $param['speed'];
        if (!empty($param['status'])) $save_data['status'] = $param['status'];
        if (!empty($param['location'])) $save_data['location'] = $param['location'];
        if (!empty($param['temperature'])) $save_data['temperature'] = $param['temperature'];
        if (!empty($param['humidity'])) $save_data['humidity'] = $param['humidity'];
        if (!empty($param['entrance_guard'])) $save_data['entrance_guard'] = $param['entrance_guard'];
        if (!empty($param['refrigerator'])) $save_data['refrigerator'] = $param['refrigerator'];
        if (!empty($param['oil'])) $save_data['oil'] = $param['oil'];
        if (!empty($param['mileage_now'])) $save_data['mileage_now'] = $param['mileage_now'];
        if (!empty($param['mileage_all'])) $save_data['mileage_all'] = $param['mileage_all'];

        $operation = $model->save($save_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 关于我们
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function aboutUs()
    {
        $return_data = C('SITE');

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$return_data);
    }

    /**
     * @desc
     * @param $param
     * @author Egor<http://gitee.com/seosite>
     */
    public function addressList($param)
    {
        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('pid');

        unset($check);

        $model = M('loclist');


        if (!empty($param['pid'])){
            $map['parent_id'] = $param['pid'];
        }else{
            $map['deep'] = 1;
        }

        list($list,$page) = _listPage($model,$map,1,null,'id,name,next_city_count','500');

        return showList($list,$page);
    }

}